package ext.generic.wfaugment;

import java.util.HashMap;
import java.util.Vector;

import wt.fc.ReferenceFactory;

import wt.project.Role;

import wt.team.Team;

import wt.util.WTException;
import wt.util.WTMessage;
import wt.util.WTProperties;

import wt.workflow.definer.WfAssignedActivityTemplate;
import wt.workflow.engine.WfActivity;
import wt.workflow.engine.WfState;
import wt.workflow.work.WorkItem;

import com.ptc.core.components.descriptor.DataUtilityHelper;
import com.ptc.core.components.descriptor.ModelContext;
import com.ptc.core.components.factory.AbstractDataUtility;
import com.ptc.core.components.rendering.guicomponents.GUIComponentArray;
import com.ptc.core.components.rendering.guicomponents.IconComponent;
import com.ptc.core.components.rendering.guicomponents.TextDisplayComponent;
import com.ptc.core.components.rendering.guicomponents.GuiComponentUtil.Delimiter;
import com.ptc.netmarkets.model.NmOid;
import com.ptc.netmarkets.util.misc.NmAction;
import com.ptc.netmarkets.util.misc.NmActionServiceHelper;

public class WorkFlowStepDataUtility extends AbstractDataUtility
{

    private static String CLASSNAME = WorkFlowStepDataUtility.class.getName();
    private static boolean VERBOSE = false;
    public static String activelabel = "";
    private static String RESOURCE = "ext.generic.wfaugment.workflowStepRB";
    private static WTProperties wtproperties;
    static
    {
        wtproperties = null;
        try
        {
            wtproperties = WTProperties.getLocalProperties();
            VERBOSE = wtproperties.getProperty("ext.generic.wfaugment.verbose", false);
            activelabel = WTMessage.getLocalizedMessage(RESOURCE, "SET_PARTICIPANTS", null);
        }
        catch (Throwable throwable)
        {
            throwable.printStackTrace();
        }
    }

    public WorkFlowStepDataUtility()
    {

    }

    public Object getDataValue(String s, Object obj, ModelContext modelcontext) throws WTException
    {
        GUIComponentArray guiComponentArray = new GUIComponentArray();
        HashMap elementsht = (HashMap) obj;
        if (elementsht == null || elementsht.size() == 0)
            return DataUtilityHelper.getDefaultDataValue(s, obj, modelcontext);
        if (s.startsWith("step_"))
        {
            WorkItem workItem = (WorkItem) elementsht.get(s + "_workItem");
            Team team = (Team) elementsht.get(s + "_team");
            WfActivity currentwfactivity = (WfActivity) elementsht.get(s + "_currentwfactivity");
            WfAssignedActivityTemplate wfactivitytemplate = (WfAssignedActivityTemplate) elementsht.get(s + "_wfAssignedActivityTemplate");
            Role role = (Role) elementsht.get(s + "_role");
            String defaultrole = (String) elementsht.get(s + "_roledefault");
            String user = (String) elementsht.get(s + "_user");
            WfActivity wfa = (WfActivity) elementsht.get(s + "_wfactivity");
            Vector setroleset = (Vector) elementsht.get(s + "_setroleset");
            if (VERBOSE)
            {
                System.out.println("***************************************");
                System.out.println("-------------------getDataValue at " + s);
                System.out.println("----team=" + team);
                System.out.println("----wfactivitytemplate=" + wfactivitytemplate.getName());
                System.out.println("----role=" + role);
                System.out.println("----defaultrole=" + defaultrole);
                System.out.println("----user=" + user);
                System.out.println("----setroleset=" + setroleset);
                if (wfa != null)
                {
                    System.out.println("----wfa=" + wfa.getName());
                    System.out.println("----wfa state=" + wfa.getState().toString());
                }
                else
                    System.out.println("----wfa=" + wfa);
            }
            boolean setable = WorkFlowStepUtil.getSetable(currentwfactivity);
            String routetyp = "";
            if (defaultrole.startsWith("Y"))
                routetyp = "Y";
            else
                routetyp = "S";
            if (setable && !workItem.isComplete() && role != null)
            {
                String dispaly = wfactivitytemplate.getName() + "(" + routetyp + ")";

                if (setroleset.contains(role.toString()))
                {
                    NmOid nmoid = new NmOid(workItem.getPersistInfo().getObjectIdentifier());
                    NmAction nmaction = NmActionServiceHelper.service.getAction("custom", "setParticipants");
                    nmaction.setContextObject(nmoid);
                    nmaction.addParam("roletype", role.toString());
                    ReferenceFactory referencefactory = new ReferenceFactory();
                    String activeoid = referencefactory.getReferenceString(wfactivitytemplate);
                    nmaction.addParam("activeoid", activeoid);
                    String s1 = "";
                    try
                    {
                        s1 = nmaction.getActionUrlExternal();
                    }
                    catch (Exception e)
                    {
                        e.printStackTrace();
                    }
                    /*
            		UrlDisplayComponent urldisplaycomponent = new UrlDisplayComponent("activename:"+s, dispaly, s1);
            		if(wfa!=null)
        			{
            			if(wfa.getState().equals(WfState.OPEN_RUNNING))
            				urldisplaycomponent.addStyleClass("myrunningactivelabel");
        				else
        					urldisplaycomponent.addStyleClass("mycompletedactivelabel");
        			}
        			else       		
        			    urldisplaycomponent.addStyleClass("mywaitingactivelabel");
        			guiComponentArray.addGUIComponent(urldisplaycomponent);
        			*/
                    TextDisplayComponent urldisplaycomponent = new TextDisplayComponent("activename:" + s);
                    urldisplaycomponent.setValue(dispaly);
                    if (wfa != null)
                    {
                        if (wfa.getState().equals(WfState.OPEN_RUNNING))
                            urldisplaycomponent.addStyleClass("myrunningactivelabel");
                        else
                            urldisplaycomponent.addStyleClass("mycompletedactivelabel");
                    }
                    else
                        urldisplaycomponent.addStyleClass("mywaitingactivelabel");
                    guiComponentArray.addGUIComponent(urldisplaycomponent);
                    guiComponentArray.setDelimiter(Delimiter.NONE);
                    IconComponent iconcomponent = new IconComponent("netmarkets/images/adduser.gif");
                    iconcomponent.setTooltip(activelabel);
                    iconcomponent.setUrl(s1);
                    guiComponentArray.addGUIComponent(iconcomponent);
                }
                else
                {
                    TextDisplayComponent textcomponent1 = new TextDisplayComponent("activename:" + s);
                    textcomponent1.setValue(dispaly);
                    if (wfa != null)
                    {
                        if (wfa.getState().equals(WfState.OPEN_RUNNING))
                            textcomponent1.addStyleClass("myrunningactivelabel");
                        else
                            textcomponent1.addStyleClass("mycompletedactivelabel");
                    }
                    else
                        textcomponent1.addStyleClass("mywaitingactivelabel");
                    guiComponentArray.addGUIComponent(textcomponent1);
                    guiComponentArray.setDelimiter(Delimiter.NONE);
                    IconComponent iconcomponent = new IconComponent("netmarkets/images/adduser.gif");
                    guiComponentArray.addGUIComponent(iconcomponent);
                }
            }
            else
            {
                TextDisplayComponent textcomponent = new TextDisplayComponent("activename:" + s);
                textcomponent.setValue(wfactivitytemplate.getName());
                if (wfa != null)
                {
                    if (wfa.getState().equals(WfState.OPEN_RUNNING))
                        textcomponent.addStyleClass("myrunningactivelabel");
                    else
                        textcomponent.addStyleClass("mycompletedactivelabel");
                }
                else
                    textcomponent.addStyleClass("mywaitingactivelabel");
                guiComponentArray.addGUIComponent(textcomponent);
                guiComponentArray.setDelimiter(Delimiter.NONE);
                IconComponent iconcomponent = new IconComponent("netmarkets/images/adduser.gif");
                guiComponentArray.addGUIComponent(iconcomponent);
            }
            guiComponentArray.setDelimiter(Delimiter.LINE_BREAK);
            TextDisplayComponent usercomponent = new TextDisplayComponent("user:" + s);
            usercomponent.setValue(user);
            guiComponentArray.addGUIComponent(usercomponent);


        }
        if (guiComponentArray.size() > 0)
            return guiComponentArray;
        return DataUtilityHelper.getDefaultDataValue(s, obj, modelcontext);
    }
}
